<?php
namespace app\mini_api\controller;

class Train extends Permission
{
    public function _initialize()
    {
        parent::_initialize();
        parent::check();
    }

    public function getList()
    {
        $page = isset($this->input_data['page']) ? $this->input_data['page'] : 1;
        $pageSize = isset($this->input_data['pageSize']) ? $this->input_data['pageSize'] : 15;

        $now = date('Y-m-d');
        $map = [
            'driver_id' => $this->user['id'],
            'b.s_time' => ['<=',$now],
            'b.e_time' => ['>=',$now],
        ];

        $trains = db('DriverTrain')->alias('a')->join('train b','a.train_id = b.id','LEFT')->field('a.*,b.title,b.duration,b.quantity,b.s_time,b.e_time')->where($map)->order('id', 'desc')->page($page,$pageSize)->select();
        $sql = db()->getLastSql();

        $total = db('DriverTrain')->alias('a')->join('train b','a.train_id = b.id','LEFT')->field('a.*,b.title,b.duration,b.quantity,b.s_time,b.e_time')->where($map)->count();
        $list = []; 

        foreach ($trains as $v) {
            $vMap = [
                'driver_train_id' => $v['id']
            ];
            $videoList = [];
            $video = db('DriverTrainVideo')->alias('a')->join('video b','a.video_id = b.id','LEFT')->where($vMap)->field('a.*,b.title,b.url,b.duration,b.create_time,b.img,b.type')->order('id DESC')->select();
            foreach ($video as $val) {
                $val['createTime'] = date('Y-m-d H:i:s',$val['create_time']);
                $val['percent'] = formatMoneyShow($val['percent']);
                $videoList[] = $val;
            }
            $v['videoList'] = $videoList;
            $v['percent'] = formatMoneyShow($v['percent']);
            $list[] = $v;
        }

        $data = [
            'list' => $list,
            'total' => $total,
            'sql' => $sql
        ];
        $this->resSuc($data);
    }


}
